rm(list=ls())
library(ggplot2)
library(dplyr)

# Input/Output filepaths NEEDS TO BE SET 
# ------------------- Set Input Files --------------------------
# Set the Path of Input QAP file - reads in QAP results table
# Follower results
input_file_name_same <- "QAP/QAP_outputs/QAP_followers_SameState_output.Rds"
input_file_name_diff <- "QAP/QAP_outputs/QAP_followers_DiffState_output.Rds"

# Mentions results
#input_file_name_same <- "QAP/QAP_outputs/QAP_mentions_SameState_output.Rds"
#input_file_name_diff <- "QAP/QAP_outputs/QAP_mentions_DiffState_output.Rds"

# Retweets results
#input_file_name_same <- "QAP/QAP_outputs/QAP_retweets_SameState_output.Rds"
#input_file_name_diff <- "QAP/QAP_outputs/QAP_retweets_DiffState_output.Rds"

# ------------------- Set Output Files --------------------------
output_file_name <- "QAP/QAP_outputs/plots/QAP_followers_plot.pdf"
#output_file_name <- "QAP/QAP_outputs/plots/QAP_mentions_plot.pdf"
#output_file_name <- "QAP/QAP_outputs/plots/QAP_retweets_plot.pdf"
-----------------------------------------------------------------

# Load results 
results_same <- readRDS(input_file_name_same)
results_same$Variable <- gsub("State Similarity", "State", results_same$Variable)
results_same$Variable <- gsub("Similarity", "Similarity Diff State", results_same$Variable)

results_diff <- readRDS(input_file_name_diff)
results_diff$Variable <- gsub("Similarity", "Similarity Same State", results_diff$Variable)

# Number of coefficients to plot 
num_coeff <- dim(results_same)[1]

# Assign an index for each coefficient 
# to help arrange labels in the plot in a specific order
results_same$index <- 1:(num_coeff)
results_diff$index <- 1:(num_coeff)

# Remove interaction term from the table
results_same <- results_same[c(1:29,34),]
results_diff <- results_diff[3:6,]

# Combined results
results <- rbind(results_same, results_diff)

# Indicator for if the coefficients is significant
results$significance <- ifelse(results$PValue<=0.05, 
                               "p<=0.05", "p>0.05")

# Remove any white space from variable names 
results$Variable <- trimws(results$Variable)

# Assign numeric values based on similarity, sender, receiver matrix 
# to keep the display order in the plots
results$sub_headings <- 6
#results[results$Variable=="State",]$sub_headings <- 1
results[grepl("Similarity Same State", results$Variable, fixed=T),]$sub_headings <- 2
results[grepl("Similarity Diff State", results$Variable, fixed=T),]$sub_headings <- 3
results[grepl("Sender", results$Variable),]$sub_headings <- 4
results[grepl("Receiver", results$Variable),]$sub_headings <- 5
results[results$Variable=="State",]$sub_headings <- 1
#results[grepl("\\* Same", results$Variable),]$sub_headings <- 5

# Rename some variables 
results$Variable <- gsub("Effect", "", results$Variable)
results$Variable <- gsub("Difference in Legislatures Profeshionalism",
                         "Professionalism Difference",
                         results$Variable)
results$Variable[grepl("\\Profesh\\b",results$Variable)] <- c("Professionalism Sender",
                                                              "Professionalism Receiver")
results$Variable[grepl("\\Dem\\b", results$Variable)] <- "Democrat Sender"
results$Variable[grepl("\\Rep\\b", results$Variable)] <- "Republican Sender"
results$Variable <- gsub("Similarity Same State", "", results$Variable)
results$Variable <- gsub("Similarity Diff State", "", results$Variable)

# Assign label groups for the coefficients
sub_heading_labs <- c (`1` = "", 
                       `2` = "Effects             \n\n(Among \nSame State)", 
                       `3` = "        Similarity\n\n(Among \nDiff State)",
                       `4` = "Sender Effects", 
                       `5` = "Receiver Effects", 
                       `6` = "Other")


# largest and smallest coefficient value 
max <- max(results$Coefficient)
min <- min(results$Coefficient)

# Create label padding for custom positioning of coefficient values
label_pad <- (max -min)/13   # Needs to be adjusted based on how the plot looks 
# retweets = 1
# mentions = 5
# follower = 13

results$lab_pad <- ifelse(results$Coefficient < 0, -label_pad, label_pad)
results$label_position <- round(results$Coefficient, 3) + results$lab_pad

# Assign plot to a variable
qap_plot <- results %>%
  mutate(name = factor(Variable)) %>%
  ggplot(aes(x=Coefficient, 
             y = reorder(name, -index),
             colour=significance)) 

# Draw plot 
qap_plot + 
  geom_point(size = 2) + 
  geom_segment(aes(xend=0, 
                   yend=name)) + 
  geom_text(label= format(round(results$Coefficient, 3), nsmall=3), 
            x = results$label_position,
            cex = 6,
            col = "black") +
  facet_grid(sub_headings ~ ., 
             scales = "free_y", 
             space = "free_y", 
             switch = "y",
             labeller=as_labeller(sub_heading_labs)) +
  coord_cartesian(xlim = c(min-1, max+1)) + 
  scale_color_manual(name = "significance",
                     values = c( "p<=0.05" = "black", 
                                 "p>0.05" = "grey")) + 
  labs(x="Coefficient Value", y = "") + 
  theme_minimal() + 
  theme(text = element_text(size = 16))

# save plot 
ggsave(plot = last_plot(),
       file = output_file_name, 
       width = 13, height = 11)

